define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'meet/order/index' + location.search,
                    add_url: 'meet/order/add',
                    edit_url: 'meet/order/edit',
                    del_url: 'meet/order/del',
                    multi_url: 'meet/order/multi',
                    import_url: 'meet/order/import',
                    image_url: 'meet/order/image',
                    table: 'meet_order',
                }
            });

            var table = $("#table");

            table.on('load-success.bs.table', function (e, data) {
                //这里可以获取从服务端获取的JSON数据
                //console.log(data);
                //这里我们手动设置底部的值
                $("#pay_amount").text(data.extend.pay_amount);
                $("#refund_deposit").text(data.extend.refund_deposit);
            });

            // 合并操作方法
            Table.api.events.operate = $.extend(Table.api.events.operate,
                {
                    'click .btn-image': function (e, value, row, index) {
                        e.stopPropagation();
                        e.preventDefault();
                        var table = $(this).closest('table');
                        var options = table.bootstrapTable('getOptions');
                        var ids = row[options.pk];
                        row = $.extend({}, row ? row : {}, {ids: ids});
                        var url = options.extend.image_url + '?order_id=' + row.id;
                        Fast.api.open(Table.api.replaceurl(url, row, table), __('Image management'), $(this).data() || {});
                    }
                }
            );

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE'},
                        // {field: 'user_id', title: __('User_id'), operate: 'LIKE'},
                        // {field: 'gender', title: __('Gender'), searchList: {"0":__('Gender 0'),"1":__('Gender 1')}, formatter: Table.api.formatter.normal},
                        // {field: 'birthday', title: __('Birthday'), operate:'RANGE', addclass:'datetimerange', autocomplete:false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'reservation_time', title: __('Reservation_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_deposit', title: __('Order_deposit'), operate: false},
                        {field: 'total_price', title: __('Total_price'), operate: false},
                        {field: 'pay_price', title: __('Pay_price'), operate: false},
                        {field: 'pay_status', title: __('Pay_status'), searchList: {"10":__('Pay_status 10'),"20":__('Pay_status 20'),"30":__('Pay_status 30')}, formatter: Table.api.formatter.status},
                        // {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'order_status', title: __('Order_status'), searchList: {"10":__('Order_status 10'),"20":__('Order_status 20'),"30":__('Order_status 30'),"40":__('Order_status 40'),"50":__('Order_status 50')},custom: {"10": "danger","20":"gray","30":"warning","40":"info","50":"success"}, formatter: Table.api.formatter.status},
                        {field: 'is_refund_deposit', title: __('Is_refund_deposit'), searchList: {"0":__('Is_refund_deposit 0'),"1":__('Is_refund_deposit 1'),custom:{"0":"gray","1":"success"}}, formatter: Table.api.formatter.status},
                        // {field: 'transaction_id', title: __('Transaction_id'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'pay_time', title: __('Pay_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons:[
                                {
                                    name: 'detail',
                                    text: __('Detail'),
                                    title: __('Detail'),
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: '',
                                    url: 'meet/order/detail',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        return true;
                                    }
                                },
                                {
                                    name: 'cancel',
                                    text: __('Cancel order'),
                                    title: __('Cancel order'),
                                    classname: 'btn btn-xs btn-danger btn-ajax',
                                    icon: '',
                                    url: 'meet/order/cancel',
                                    confirm: __('Are you sure you want to cancel your order?'),
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh',{});//刷新表格
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.order_status == 10){
                                            return true
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'modifytime',
                                    text: __('Modify time'),
                                    title: __('Modify time'),
                                    classname: 'btn btn-xs btn-danger btn-dialog btn-modifytime',
                                    icon: '',
                                    url: 'meet/order/modifytime',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.order_status == 30){
                                            return true
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'progress',
                                    text: __('In progress'),
                                    title: __('In progress'),
                                    classname: 'btn btn-xs btn-info btn-ajax',
                                    icon: '',
                                    url: 'meet/order/progress',
                                    confirm: __('Are you sure to change the order status to Ongoing?'),
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh',{});//刷新表格
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.order_status == 30){
                                            return true
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'deposit',
                                    text: __('Refund deposit'),
                                    title: __('Refund deposit'),
                                    classname: 'btn btn-xs btn-primary btn-ajax',
                                    icon: '',
                                    url: 'meet/order/refunddeposit',
                                    confirm: __('Are you sure you want your deposit back?'),
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh',{});//刷新表格
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if((row.order_status == 30 || row.order_status == 40) && row.is_refund_deposit == 0 && row.order_deposit > 0){
                                            return true
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'complete',
                                    text: __('Complete order'),
                                    title: __('Complete order'),
                                    classname: 'btn btn-xs btn-success btn-ajax',
                                    icon: '',
                                    url: 'meet/order/complete',
                                    confirm: __('Are you sure you want to complete your order?'),
                                    success: function (data, ret) {
                                        table.bootstrapTable('refresh',{});//刷新表格
                                        //如果需要阻止成功提示，则必须使用return false;
                                        //return false;
                                    },
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.order_status == 30 || row.order_status == 40){
                                            return true
                                        }
                                        return false;
                                    }
                                },
                                {
                                    name: 'image',
                                    text: __('Image management'),
                                    title: __('Image management'),
                                    classname: 'btn btn-xs btn-primary btn-image',
                                    icon: '',
                                    url: '',
                                    visible: function (row) {
                                        //返回true时按钮显示,返回false隐藏
                                        if(row.order_status > 20){
                                            return true
                                        }
                                        return false;
                                    }
                                },
                            ]
                        }
                    ]
                ],
                onLoadSuccess: function () {
                    $(".btn-add").data("area", ['60%','60%']);
                    $(".btn-dialog").data("area", ['60%','80%']);
                    $(".btn-modifytime").data("area", ['40%','40%']);
                    $('.btn-image').data("area", ['60%','80%']);
                }
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        modifytime: function (){
            Controller.api.bindevent();
        },
        image: function (){
            $(".faupload").data("upload-complete", function (files) {
                var length = files.length;
                var nums = 0;
                for (var i = 0; i < length; i++){
                    if(files[i].ret.code == 1){
                        Fast.api.ajax({
                            url: 'meet/order/relatedImage',
                            data: {
                                order_id: $('#order_id').val(),
                                image: files[i].ret.data.url,
                            }
                        }, function (data, ret) {
                            if(nums == length - 1){
                                setTimeout(function () {
                                    $("#table-image").bootstrapTable('refresh');
                                }, 100);
                            }
                            nums++;
                            return false
                        });
                    }

                }
            });

            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'meet/order/image' + location.search,
                    del_url: 'meet/order/delImage',
                    table: 'meet_order_images',
                }
            });

            var table = $("#table-image");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                search: false,
                commonSearch: false,
                showToggle: false,
                showColumns: false,
                showExport: false,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);

            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
